package com.dokimall.dokimallseckill.Controller;


import com.dokimall.common.utils.R;
import com.dokimall.dokimallseckill.service.SeckillService;
import com.dokimall.dokimallseckill.vo.SeckillSessionVo;
import com.dokimall.dokimallseckill.vo.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSku")
    public R getCurrentSeckillSku(){
        log.info("currentSeckillSku正在执行中....");
        List<SeckillSkuRedisTo> seckillSessionVos = seckillService.getCurrentSeckillSku();
        return R.ok().setData(seckillSessionVos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo seckillSkuRedisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(seckillSkuRedisTo);
    }

    @GetMapping("/kill")
    public String killItem(@RequestParam("killId")String killId, @RequestParam("key")String key, @RequestParam("num")Integer num, Model model){
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);

        return "success";
    }

}
